import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';

import '../utils/StringUtils.dart';
import '../utils/utils.dart';
import '../utils/widget_utils.dart';

/**
 * 导出多语言
 */
class TranslateExportView extends StatefulWidget {
  final String resPath;

  TranslateExportView({Key? key, required this.resPath}) : super(key: key);

  @override
  _TranslateExportViewState createState() => _TranslateExportViewState();
}

class _TranslateExportViewState extends State<TranslateExportView> {
  final TextEditingController _controller_project = TextEditingController();
  final TextEditingController _controller_res_path = TextEditingController();
  final TextEditingController _controller_error_path = TextEditingController();

  @override
  void initState() {
    super.initState();
    setState(() {
      _controller_res_path.text = widget.resPath.trim();
    });
  }

  /**
   * 选择文件
   */
  Future<void> _pickFile() async {
    FilePickerResult? result = await FilePicker.platform.pickFiles();
    if (result != null) {
      setState(() {
        _controller_error_path.text = result.files.single.path!;
      });
    } else {
      // 用户取消了选择
      setState(() {
        _controller_error_path.text = "";
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return SizedBox(
        width: 800,
        height: 230,
        child: Column(children: [
          Row(
            children: [
              getTextWidget(16, "项目名称   ", Colors.black),
              SizedBox(width: 18),
              Expanded(
                  child: TextField(
                controller: _controller_project,
                decoration: InputDecoration(
                  labelText: '请输入项目名称',
                  hintText: '请输入项目名称',
                  border: OutlineInputBorder(),
                ),
              )),
            ],
          ),
          SizedBox(
            height: 16,
          ),
          Row(
            children: [
              getTextWidget(16, "多语言路径", Colors.black),
              SizedBox(width: 18),
              Expanded(
                  child: TextField(
                controller: _controller_res_path,
                decoration: InputDecoration(
                  labelText: '请输入多语言路径',
                  hintText: '请输入多语言路径',
                  border: OutlineInputBorder(),
                ),
              )),
            ],
          ),
          SizedBox(
            height: 16,
          ),
          Row(children: [
            getTextWidget(16, "故障码路径", Colors.black),
            SizedBox(width: 18),
            Expanded(
                child: TextField(
              controller: _controller_error_path,
              decoration: InputDecoration(
                labelText: '请输入故障码文件路径',
                hintText: '请输入故障码文件路径',
                border: OutlineInputBorder(),
              ),
            )),
            SizedBox(
              width: 16,
            ),
            SizedBox(
                height: 40,
                child: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.blue,
                    ),
                    onPressed: () {
                      _pickFile();
                    },
                    child: getTextWidget(16, "文件选择", Colors.white)))
          ]),
          SizedBox(
            height: 16,
          ),
          Row(
            children: [
              Expanded(
                  child: SizedBox(
                height: 1,
              )),
              TextButton(
                child: Text("导出"),
                onPressed: () {
                  String project_name = _controller_project.text.trim();
                  String res = _controller_res_path.text.trim();
                  String json = _controller_error_path.text.trim();
                  if (project_name.isEmpty) {
                    showToast("项目名称不能为空");
                    return;
                  }
                  if (res.isEmpty) {
                    showToast("多语言路径不能为空");
                    return;
                  }
                  if (json.isEmpty) {
                    showToast("故障码路径不能为空");
                    return;
                  }
                  StringsUtils.exportLanguage(project_name, res, json);
                  Navigator.pop(context);
                },
              ),
              TextButton(
                child: Text("取消"),
                onPressed: () {
                  Navigator.pop(context);
                },
              ),
            ],
          )
        ]));
  }
}
